package servlet;

import database.MySQLUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

@WebServlet("/check_usr_pwd")
public class MyServlet_checkUsrPwd extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String username = req.getParameter("username");
        String pwd = req.getParameter("password");
        if (username != null && pwd != null) {
            String sql = "select * from user where mobile='" + username + "'";
            ResultSet resultSet = null;
            try {
                resultSet = MySQLUtils.getResultSet(sql);
                if (!resultSet.next()) {
                    out.print("用户名不存在");
                } else {
                    sql = "select * from user where mobile='" + username + "'" + " and password='" + pwd + "'";
                    resultSet = MySQLUtils.getResultSet(sql);
                    if (!resultSet.next()) {
                        out.print("密码错误");
                    } else {
                        out.print("登录成功");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
